﻿namespace HiMap.MapControls.Tools
{
    using HiMap.Carto;
    using HiMap.Geometry;
    using HiMap.MapControls;
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Windows.Forms;

    public class EditGeometry
    {
        protected Button _bt2;
        protected Button _bt3;
        protected Button _btExit;
        protected ComboBox _cmb;
        private IFeature _feat;
        protected Form _frm;
        protected IPoint _GPSPoint;
        protected IFeatureLayer _layer;
        protected Panel _pl;
        protected bool isMouseDown;
        protected List<IPoint> listPoint;
        protected List<Point> listPoint1;
        protected MapControl Map;
        private PicCtrl MePic;
        protected PictureBox picSd;
        protected int StaX;
        protected int StaY;

        public EditGeometry(Form frm, MapControl map)
        {
            this._frm = frm;
            this.Map = map;
            this.isMouseDown = false;
            this.picSd = null;
            this._GPSPoint = null;
        }

        private void Btn2Click(object sender, EventArgs e)
        {
            if (this._cmb.SelectedItem.ToString() == "All")
            {
                this.BtnExitClick(null, null);
            }
            else
            {
                this.MePic.Delete(this._cmb.SelectedIndex - 1);
                this._cmb.Items.RemoveAt(this._cmb.SelectedIndex);
                this.picSd = null;
                this._cmb.SelectedIndex = this._cmb.Items.Count - 1;
            }
        }

        protected virtual void Btn3Click(object sender, EventArgs e)
        {
            IGeometry geometry = null;
            IPointCollection points = null;
            switch (this._feat.Shape.Type)
            {
                case GeometryType.Point:
                    if (this.listPoint.get_Count() > 0)
                    {
                        geometry = this.listPoint.get_Item(0);
                    }
                    break;

                case GeometryType.Polyline:
                    points = new PolylineClass();
                    break;

                case GeometryType.Polygon:
                    points = new PolygonClass();
                    break;
            }
            if (this.listPoint.get_Count() == 0)
            {
                this._feat.Shape = null;
            }
            else
            {
                if (geometry == null)
                {
                    for (int i = 0; i < this.listPoint.get_Count(); i++)
                    {
                        points.AddPoint(this.listPoint.get_Item(i));
                    }
                    geometry = points;
                }
                this._feat.Shape = geometry;
            }
            this._feat.Save();
            this.Map.MapRefresh();
            this._pl.Visible = false;
            this.BtnExitClick(null, null);
        }

        protected virtual void BtnExitClick(object sender, EventArgs e)
        {
            this.listPoint.Clear();
            this.listPoint1.Clear();
            this.MePic.Clear(false);
            this.Map.MapRefresh();
            this._pl.Visible = false;
            this._cmb.Items.Clear();
            this._cmb.Items.Add("All");
            this._cmb.SelectedIndex = 0;
        }

        private void CmbSelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.picSd != null)
            {
                this.picSd.BackColor = Color.Red;
            }
            if (this._cmb.SelectedItem.ToString() != "All")
            {
                this.MePic[this._cmb.SelectedIndex - 1].BackColor = Color.Blue;
                this.picSd = this.MePic[this._cmb.SelectedIndex - 1];
            }
        }

        private void CreateControls()
        {
            this._pl = new Panel();
            this._bt2 = new Button();
            this._bt3 = new Button();
            this._btExit = new Button();
            this._cmb = new ComboBox();
            this._pl.Width = 240;
            this._pl.Height = 0x1b;
            this._pl.BackColor = Color.FromArgb(0xc0, 0xc0, 0xc0);
            this._pl.MouseDown += new MouseEventHandler(this.panCtrl_MouseDown);
            this._pl.MouseMove += new MouseEventHandler(this.panCtrl_MouseMove);
            this._pl.MouseUp += new MouseEventHandler(this.panCtrl_MouseUp);
            this._pl.Visible = false;
            this._frm.Controls.Add(this._pl);
            this._frm.Controls.SetChildIndex(this._pl, 0);
            this._cmb.Location = new Point(3, 3);
            this._cmb.Width = 0x4b;
            this._cmb.Height = 0x16;
            this._cmb.SelectedIndexChanged += new EventHandler(this.CmbSelectedIndexChanged);
            this._pl.Controls.Add(this._cmb);
            this._bt2.Text = "Del";
            this._bt2.Location = new Point(0x54, 3);
            this._bt2.Width = 0x2f;
            this._bt2.Height = 20;
            this._bt2.Click += new EventHandler(this.Btn2Click);
            this._pl.Controls.Add(this._bt2);
            this._bt3.Text = "Save";
            this._bt3.Location = new Point(0x89, 3);
            this._bt3.Width = 0x2f;
            this._bt3.Height = 20;
            this._bt3.Click += new EventHandler(this.Btn3Click);
            this._pl.Controls.Add(this._bt3);
            this._btExit.Text = "Cancel";
            this._btExit.Location = new Point(190, 3);
            this._btExit.Width = 0x2f;
            this._btExit.Height = 20;
            this._btExit.Click += new EventHandler(this.BtnExitClick);
            this._pl.Controls.Add(this._btExit);
            this._cmb.Items.Add("All");
            this._cmb.SelectedIndex = 0;
        }

        private void panCtrl_MouseDown(object sender, MouseEventArgs e)
        {
            this.StaX = e.X;
            this.StaY = e.Y;
            this.isMouseDown = true;
        }

        private void panCtrl_MouseMove(object sender, MouseEventArgs e)
        {
            if (this.isMouseDown)
            {
                this._pl.Location = new Point(0, (this._pl.Location.Y + e.Y) - this.StaY);
            }
        }

        private void panCtrl_MouseUp(object sender, MouseEventArgs e)
        {
            this.isMouseDown = false;
        }

        private void setCmd()
        {
            int num = this.listPoint1.get_Count() - 1;
            this._cmb.Items.Add("Point" + num.ToString());
            this._cmb.SelectedIndex = this._cmb.Items.Count - 1;
        }

        public void Show(IFeature feat)
        {
            this.CreateControls();
            this._feat = feat;
            this.listPoint = new List<IPoint>();
            this.listPoint1 = new List<Point>();
            IGeometry geometry = null;
            IPointCollection points = null;
            switch (feat.Shape.Type)
            {
                case GeometryType.Point:
                {
                    this.MePic = new PicCtrl(this.Map, this.listPoint, this.listPoint1, PicType.Point);
                    IPoint shape = (IPoint) feat.Shape;
                    this.listPoint.Add(shape);
                    this.listPoint1.Add(this.Map.MapToScreen(shape.X, shape.Y));
                    this.MePic.CreatePic(this.listPoint1.get_Item(this.listPoint1.get_Count() - 1).X, this.listPoint1.get_Item(this.listPoint1.get_Count() - 1).Y);
                    this.setCmd();
                    break;
                }
                case GeometryType.Polyline:
                    this.MePic = new PicCtrl(this.Map, this.listPoint, this.listPoint1, PicType.Polyline);
                    geometry = feat.Shape;
                    break;

                case GeometryType.Polygon:
                    this.MePic = new PicCtrl(this.Map, this.listPoint, this.listPoint1, PicType.Polygon);
                    geometry = feat.Shape;
                    break;
            }
            if (geometry != null)
            {
                IPoint point = null;
                points = (IPointCollection) geometry;
                for (int i = 0; i < points.PointCount; i++)
                {
                    point = points.GetPoint(i);
                    this.listPoint.Add(point);
                    this.listPoint1.Add(this.Map.MapToScreen(point.X, point.Y));
                    this.MePic.CreatePic(this.listPoint1.get_Item(this.listPoint1.get_Count() - 1).X, this.listPoint1.get_Item(this.listPoint1.get_Count() - 1).Y);
                    this.setCmd();
                }
            }
            this.MePic.Draw();
            this._pl.Visible = true;
        }

        private void test()
        {
            this.listPoint.set_Item(this._cmb.SelectedIndex, this.GPSPoint);
            this.listPoint1.set_Item(this._cmb.SelectedIndex, this.Map.MapToScreen(this.GPSPoint.X, this.GPSPoint.Y));
            this.MePic.MovePic(this._cmb.SelectedIndex);
        }

        public IPoint GPSPoint
        {
            get
            {
                return this._GPSPoint;
            }
            set
            {
                this._GPSPoint = value;
            }
        }
    }
}

